package com.dayuanit.dy17.library.util;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.InputStream;

public class DBUtils {
    private static SqlSessionFactory ssf;

    static {
        init();
    }

    private static void init() {
        try {
            InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
            ssf = new SqlSessionFactoryBuilder().build(is);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //默认sqlSession开启事务
    public static SqlSession getSqlSession() {
        return ssf.openSession();
    }

    public static SqlSession getSqlSession(boolean autoCommit) {
        return ssf.openSession(autoCommit);
    }
}
